package controllers;

import models.UserDTO;
import services.UserServiceImpl;

public class Login_Controller {

	UserDTO user;
	
    public boolean Verify(String login, String Password) {
    	
        boolean loginWithSuccess = false;
        
        try {
        	
        	if(login != "" && Password != "")
        	{ 
        		if(login == "sami" && Password == "sami") //This condition should be delete after finishing testing
        		{
        			loginWithSuccess = true;
        		}
        		else
        		{   			
        			UserServiceImpl usi = new UserServiceImpl();
        			user = usi.GetByLogin(login);
	        	
        			if(user.getPassword() != null){
        				if(user.getPassword().equalsIgnoreCase(Password)) 
        					loginWithSuccess = true;
        			}
        		}
        		
        	}
           
        } catch (Exception e) {
        	
            System.out.println("Failed on Login_Controller/Verify function:" + e.getMessage());
           
        }
        return loginWithSuccess;
    }

    public boolean isAdmin()
    {
    	if(user != null){
    		
    		int roleId = user.getIdRole();
    		if (roleId == 1 ) return true;
    		else return false;		
    	}
    	else return false;
    }
    
    public boolean AddLog(UserDTO newUser) {
    	boolean succeeded = false;
    	
        try {
            
        	UserServiceImpl userService = new UserServiceImpl();
        	
        	if(userService.GetByLogin(newUser.getLogin()) == null)
        	{
        		userService.Add(newUser);
        		succeeded = true;
        	}
            
        } catch (Exception ex) {
            System.out.println("Failed:" + ex.getMessage());
        }
        return succeeded;
    }

    public static void main(String[] args) {
        //  LogInController l = new LogInController();
        //System.out.print("" + l.VerifyMember("joujou", "jjjjjj"));
        Login_Controller c = new Login_Controller();
        c.Verify("hoda", "123");
       //   Login log=new Login("Hoda","123");
       // c.AddLog(log);
    }
}
